package com.bs.bszhsq.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.bszhsq.dto.UsersDTO;
import com.bs.bszhsq.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import javafx.scene.input.DataFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.bs.bszhsq.utils.R;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.bs.bszhsq.service.IUsersService;
import com.bs.bszhsq.entity.Users;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 章宇光
 * @since 2024-03-17
 */
@RestController
@RequestMapping("/users")
@Api(tags = "用户相关接口")
public class UsersController {
    //service注入
    @Resource
    private IUsersService usersService;


    @PostMapping("/register")
    @ApiOperation("注册")
    public R register(@RequestBody UsersDTO usersDTO) {
        String username = usersDTO.getUsername();
        String password = usersDTO.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return R.error("参数错误！");
        }
        boolean save = usersService.saveUser(usersDTO);
        return R.write(save);
    }


    @PostMapping("/login")
    @ApiOperation(value = "登录接口")
    public R login(@RequestBody UsersDTO usersDTO) {
        String username = usersDTO.getUsername();
        String password = usersDTO.getPassword();
        String role = usersDTO.getRole();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return R.error("请输入用户名或者密码！");
        }
        if (StringUtils.isEmpty(role)) {
            return R.error("请选择角色！");
        }
        return R.success(usersService.login(usersDTO));
    }

    @PostMapping
    @ApiOperation(value = "新增")
    public R save(@RequestBody Users users) {
        boolean save = usersService.save(users);
        return R.write(save);
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody Long id) {
        boolean b = usersService.removeById(id);
        return R.write(b);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody Users users) {
        boolean update = usersService.updateById(users);
        return R.write(update);
    }


    @PostMapping("/deletebatch")
    @ApiOperation(value = "批量删除")
    public R deletebatch(@RequestBody List<Long> ids) {
        boolean b = usersService.removeByIds(ids);
        return R.write(b);
    }


    @GetMapping("/page")
    @ApiOperation(value = "客户分页查询")
    public R page(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        IPage<Users> IPage = usersService.page(new Page<>(pageNum, pageSize));
        return R.success(IPage);
    }

    @GetMapping("/{username}")
    @ApiOperation("根据用户username查询")
    public R getInfo(@PathVariable String username) {
        LambdaQueryWrapper<Users> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Users::getUsername, username);
        Users one = null;
        try {
            one = usersService.getOne(lqw);
        } catch (Exception e) {
            throw new ServiceException("500", "服务开小差了");
        }
        return R.success(one);
    }


}
